package com.hotchpotch.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.cretin.www.cretinautoupdatelibrary.utils.CretinAutoUpdateUtils;
import com.hotchpotch.R;
import com.hotchpotch.base.BaseActivity;
import com.hotchpotch.base.DemoContext;
import com.hotchpotch.base.MyApplication;
import com.hotchpotch.okHttp.MyOkHttp;
import com.hotchpotch.util.InstallUtils;
import com.hotchpotch.util.Logg;
import org.json.JSONException;
import org.json.JSONObject;
import okhttp3.FormBody;
import okhttp3.RequestBody;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = MainActivity.class.getSimpleName();
    private Button buttonA, buttonB, button3, button4, button5, button6;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        buttonA = (Button) findViewById(R.id.btn_one);
        buttonB = (Button) findViewById(R.id.btn_two);
        button3 = (Button) findViewById(R.id.btn_three);
        button4 = (Button) findViewById(R.id.btn_four);
        button5 = (Button) findViewById(R.id.btn_five);
        button6 = (Button) findViewById(R.id.btn6);
    }

    @Override
    protected void initEvents() {
        buttonA.setOnClickListener(this);
        buttonB.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        //apk更新
//        CretinAutoUpdateUtils.getInstance(MainActivity.this).check();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_one:
                goToPage(MainActivity.this, OneActivity.class, null);
                break;
            case R.id.btn_two:
                goToPage(MainActivity.this, TwoActivity.class, null);
                break;
            case R.id.btn_three:
                break;
            case R.id.btn_four://加密解密
                goToPage(MainActivity.this, EncryptAndDecryptActivity.class, null);
                break;
            case R.id.btn_five://recyclerview
                goToPage(MainActivity.this, RecyclerViewRefreshActivity.class, null);
                break;
            case R.id.btn6://bottomnavigationbar
                break;
        }
    }

    /**
     * 更新Apk
     */
    //最新APK的下载地址
    public static final String APK_URL = "http://mobile.ac.qq.com/qqcomic_android.apk";
    //下载后的APK的命名
    public static final String APK_NAME = "update";
    private void checkUpdate() {
        new InstallUtils(MainActivity.this, APK_URL, APK_NAME, new InstallUtils.DownloadCallBack() {
            @Override
            public void onStart() {
                Logg.i(TAG, "InstallUtils---onStart");
//            tv_progress.setText("0%");//进度
            }

            @Override
            public void onComplete(String path) {
                Logg.i(TAG, "InstallUtils---onComplete:" + path);

                /**
                 * 安装APK工具类
                 * @param context       上下文
                 * @param filePath      文件路径
                 * @param authorities   ---------Manifest中配置provider的authorities字段---------
                 * @param callBack      安装界面成功调起的回调
                 */
                InstallUtils.installAPK(MainActivity.this, path, getPackageName() + ".fileProvider", new InstallUtils.InstallCallBack() {
                    @Override
                    public void onSuccess() {
                        showToast("正在安装");
                    }

                    @Override
                    public void onFail(Exception e) {
                        showToast("安装失败");
                    }
                });
//            tv_progress.setText("100%");//进度
            }

            @Override
            public void onLoading(long total, long current) {
                Logg.i(TAG, "InstallUtils----onLoading:-----total:" + total + ",current:" + current);
//            tv_progress.setText((int) (current * 100 / total) + "%");//进度
            }

            @Override
            public void onFail(Exception e) {
                Logg.i(TAG, "InstallUtils---onFail:" + e.getMessage());
            }

        }).downloadAPK();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CretinAutoUpdateUtils.getInstance(this).destroy();//销毁
    }
}
